package ee.siimkaspar.subjects.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.apache.log4j.Logger;
import org.codehaus.jackson.annotate.JsonIgnoreProperties;

import com.opensymphony.xwork2.ActionContext;

import ee.siimkaspar.subjects.service.BaseService;
import ee.siimkaspar.subjects.util.Const;

@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
@Entity
@Table(name = "enterprise")
public class Enterprise implements Serializable, ModelInterface {

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name = "enterprise", unique = true, nullable = false)
	private Integer id;
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "updated_by")
	private Employee employeeByUpdatedBy;
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "created_by")
	private Employee employeeByCreatedBy;
	@Column(name = "name")
	private String name;
	@Column(name = "full_name")
	private String fullName;
	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "created", length = 29)
	private Date created;
	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "updated", length = 29)
	private Date updated;
	@OneToMany(fetch = FetchType.LAZY, mappedBy = "enterprise")
	private List<EnterprisePersonRelation> enterprisePersonRelations = new ArrayList<EnterprisePersonRelation>();
	@OneToMany(fetch = FetchType.LAZY, mappedBy = "enterprise")
	private List<Employee> employees = new ArrayList<Employee>();

	public Enterprise() {
	}

	public Enterprise(String name, String fullName) {
		super();
		this.name = name;
		this.fullName = fullName;
	}
	
	@PreUpdate
    @PrePersist
    public void updateMetadata() {
        setUpdated(new Date());
        if (getCreated()==null) {
        	setCreated(new Date());
        }
        Map<String, Object> session = ActionContext.getContext().getSession();
        if (session != null) {
            Employee p = ((UserAccount) session.get(Const.SESSION_USER)).getEmployeeBySubjectFk();
            if (p != null) {
            	setEmployeeByUpdatedBy(p);
            	if (getEmployeeByCreatedBy() == null) {
            		setEmployeeByCreatedBy(p);
            	}
            }
        } else {
            Logger.getLogger(BaseService.class).warn("Session is null. Username not set.");
        }
    }

	public Integer getId() {
		return this.id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public Employee getEmployeeByUpdatedBy() {
		return this.employeeByUpdatedBy;
	}

	public void setEmployeeByUpdatedBy(Employee employeeByUpdatedBy) {
		this.employeeByUpdatedBy = employeeByUpdatedBy;
	}

	public Employee getEmployeeByCreatedBy() {
		return this.employeeByCreatedBy;
	}

	public void setEmployeeByCreatedBy(Employee employeeByCreatedBy) {
		this.employeeByCreatedBy = employeeByCreatedBy;
	}

	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getFullName() {
		return this.fullName;
	}

	public void setFullName(String fullName) {
		this.fullName = fullName;
	}

	public Date getCreated() {
		return this.created;
	}

	public void setCreated(Date created) {
		this.created = created;
	}

	public Date getUpdated() {
		return this.updated;
	}

	public void setUpdated(Date updated) {
		this.updated = updated;
	}

	public List getEnterprisePersonRelations() {
		return enterprisePersonRelations;
	}

	public void setEnterprisePersonRelations(List enterprisePersonRelations) {
		this.enterprisePersonRelations = enterprisePersonRelations;
	}

	public List getEmployees() {
		return employees;
	}

	public void setEmployees(List employees) {
		this.employees = employees;
	}
	@Override
	public boolean validate() {
		// TODO Auto-generated method stub
		return true;
	}
}
